<?php

namespace app\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
use app\extend\Client\StatisticClient;

class StatisticByWeb implements MiddlewareInterface
{
    public function process(Request $request, callable $next): Response
    {
        $ip         = $request->getRealIp(true);
        $controller = $request->controller;
        $action     = $request->action;
        $transfer   = $controller . '::' . $action;
        $project    = 'OwenWeb项目Web';
        // 开始计时
        $unique   = StatisticClient::tick($project, $ip, $transfer);
        $response = $next($request);
        $code     = $response->getStatusCode();
        $success  = $code < 400;
        $details  = [
            'ip'              => $request->getRealIp(true) ?? '',                                             // 请求客户端IP
            'url'             => $request->fullUrl() ?? '',                                                             // 请求URL
            'method'          => $request->method() ?? '',                                                              // 请求方法
            'request_param'   => $request->all() ?? [],                                                                 // 请求参数
            'request_header'  => $request->header() ?? [],                                                              // 请求头
            'cookie'          => $request->cookie() ?? [],                                                              // 请求cookie
            'session'         => $request->session()->all() ?? [],                                                      // 请求session
            'response_code'   => $response->getStatusCode() ?? '',                                                      // 响应码
            'response_header' => $response->getHeaders() ?? [],                                                         // 响应头
            'response_body'   => $response->rawBody() ? json_decode($response->rawBody(), true) : [],             // 响应数据（发生异常）
        ];
        // 数据上报
        StatisticClient::report($unique, $project, $ip, $transfer, $success, $code, json_encode($details, JSON_PRESERVE_ZERO_FRACTION | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE));
        return $response;
    }
}